#include "TextureAtlasAsset.h"
#include <algorithm>

TextureAtlasAsset::TextureAtlasAsset(const std::string& id, const std::wstring& pathTemplate, int frameCount)
    : Asset(id, AssetType::TextureAtlas), m_pathTemplate(pathTemplate), m_frameCount(frameCount)
{
}

TextureAtlasAsset::~TextureAtlasAsset()
{
    Unload();
}

bool TextureAtlasAsset::Load()
{
    if (m_isLoaded)
        return true;

    const bool hasPlaceholder = (m_pathTemplate.find(L"%d") != std::wstring::npos);

    wchar_t path[256];
    bool success = true;
    if (hasPlaceholder)
    {
        for (int i = 0; i < m_frameCount; i++)
        {
            IMAGE* frame = new IMAGE();
            swprintf_s(path, m_pathTemplate.c_str(), i );

            loadimage(frame, path);

            // 通过检查图像的宽度和高度来判断是否加载成功
            if (frame->getwidth() == 0 || frame->getheight() == 0)
            {
                delete frame;
                success = false;
                break;
            }

            m_frames.push_back(frame);
        }
    }
    else
    {
        IMAGE TotalAtlas;
        swprintf_s(path, m_pathTemplate.c_str());
        loadimage(&TotalAtlas, path);
        int width = TotalAtlas.getwidth();
        int height = TotalAtlas.getheight();
        int wid_frame = width / m_frameCount;

        if (width <= 0 || height <= 0)
        {
            success = false;
            return false;
        }

        for (size_t i = 0; i < m_frameCount; i++)
        {
            IMAGE * temp = new IMAGE();
            SetWorkingImage(&TotalAtlas);
            getimage(temp, i * wid_frame, 0, wid_frame, height);
            SetWorkingImage(nullptr);
            

            if (temp->getheight() <= 0 || temp->getwidth() <= 0)
            {
                delete temp;
                success = false;
                break;
            }
            m_frames.push_back(temp);
        }

    }
    if (!success)
    {
        Unload();
        return false;
    }
    m_isLoaded = true;
    return true;
}

void TextureAtlasAsset::Unload()
{
    for (IMAGE* frame : m_frames)
    {
        delete frame;
    }
    m_frames.clear();
    m_isLoaded = false;
}

IMAGE* TextureAtlasAsset::GetFrame(int index) const
{
    if (index < 0 || index >= m_frames.size())
        return nullptr;

    return m_frames[index];
}

void TextureAtlasAsset::AddFrame(const IMAGE& image)
{
    IMAGE* newFrame = new IMAGE();
    *newFrame = image;
    m_frames.push_back(newFrame);
}

std::shared_ptr<TextureAtlasAsset> TextureAtlasAsset::CreateFlipped(const std::string& newId) const
{
    std::shared_ptr<TextureAtlasAsset> flippedAtlas =std::make_shared<TextureAtlasAsset>(newId, L"", 0);

    for (IMAGE* frame : m_frames)
    {
        int width = frame->getwidth();
        int height = frame->getheight();

        IMAGE* flippedFrame = new IMAGE();
        Resize(flippedFrame, width, height);

        DWORD* srcBuffer = GetImageBuffer(frame);
        DWORD* dstBuffer = GetImageBuffer(flippedFrame);

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                dstBuffer[y * width + (width - 1 - x)] = srcBuffer[y * width + x];
            }
        }

        flippedAtlas->m_frames.push_back(flippedFrame);
    }

    flippedAtlas->m_isLoaded = true;
    return flippedAtlas;
}